package com.sgb.kjwl.viewmodel.carrierDemand;

import androidx.databinding.ObservableField;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.transport.CarrierListResultEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.carrierDemand.CarrierDemandActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarrierDemandAtvModel extends BaseViewModel {
    private CarrierDemandActivity activity;
    private HomeRepertory repertory;
    public ObservableField<String> messageNum = new ObservableField<>("");
    public ObservableField<GoodsSourceFilterEntity> filterEntity = new ObservableField<>(new GoodsSourceFilterEntity());
    public ObservableField<GoodsSourceLocationEntity> sendLocation = new ObservableField<>(new GoodsSourceLocationEntity());
    public ObservableField<GoodsSourceLocationEntity> purposeLocation = new ObservableField<>(new GoodsSourceLocationEntity());
    public ObservableField<GoodsInfoTypeEntity3> carType = new ObservableField<>(new GoodsInfoTypeEntity3());
    public ObservableField<GoodsInfoTypeEntity3> carLength = new ObservableField<>(new GoodsInfoTypeEntity3());

    private int pageSize = 20;

    public CarrierDemandAtvModel(CarrierDemandActivity carrierDemandActivity, HomeRepertory repertory) {
        this.activity = carrierDemandActivity;
        this.repertory = repertory;
    }

    public void getGoodsTypeData() {

        String carsTypeString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_TYPE, "");
        String carsLengthString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_LENGTH, "");
        List<GoodsInfoTypeEntity3> carsType = new Gson().fromJson(carsTypeString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        List<GoodsInfoTypeEntity3> carsLengthType = new Gson().fromJson(carsLengthString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        activity.getCarsTypeData(carsType);
        activity.getCarsLengthData(carsLengthType);

    }

    //承运信息
    public void getCarrierInfoList(int pageNum) {

        Map map = new HashMap();
        map.put("areaCode", repertory.getAreaCode());
        map.put("page", pageNum);
        map.put("limit", pageSize);
        map.put("radius", "");
        map.put("timeSort", 1);
      /*  if (null != filterEntity.get()) {
            if (null == filterEntity.get().getCarType()) {
                map.put("carTypeNos", new ArrayList().toArray());
            } else {
                List lt = new ArrayList();
                for (GoodsInfoTypeEntity3 entity : filterEntity.get().getCarType()) {
                    if ("".equals(entity.getNo()) || "0".equals(entity.getNo()))
                        continue;
                    lt.add(entity.getNo());
                }
                map.put("carTypeNos", lt.toArray());
            }

            if (null == filterEntity.get().getLengthType()) {
                map.put("carLenNos", new ArrayList().toArray());
            } else {
                List lt = new ArrayList();
                for (GoodsInfoTypeEntity3 entity : filterEntity.get().getLengthType()) {
                    if ("".equals(entity.getNo()) || "0".equals(entity.getNo()))
                        continue;
                    lt.add(entity.getNo());
                }
                map.put("carLenNos", lt.toArray());
            }
        }*/

        if(null!=carType.get()){
            if(TextUtils.isEmpty(carType.get().getNo())){
                List lt = new ArrayList();
                map.put("carTypeNos",lt);
            }else{
                List lt = new ArrayList();
                lt.add(carType.get().getNo());
                map.put("carTypeNos",lt);
            }
        }

        if(null!=carLength.get()){
            if(TextUtils.isEmpty(carLength.get().getNo())){
                List lt = new ArrayList();
                map.put("carLenNos",lt);
            }else{
                List lt = new ArrayList();
                lt.add(carLength.get().getNo());
                map.put("carLenNos",lt);
            }
        }



        if (null != sendLocation.get()) {
            if (TextUtils.isEmpty(sendLocation.get().getProvinceNo())) {
                map.put("provinceNo", "");
            } else {
                map.put("provinceNo", sendLocation.get().getProvinceNo());
            }
        }

        if (null != sendLocation.get()) {
            if (TextUtils.isEmpty(sendLocation.get().getCityNo())) {
                map.put("cityNo", "");
            } else {
                map.put("cityNo", sendLocation.get().getCityNo());
            }
        }

        if (null != sendLocation.get()) {
            if (TextUtils.isEmpty(sendLocation.get().getAreaNo())) {
                map.put("districtNo", "");
            } else {
                map.put("districtNo", sendLocation.get().getAreaNo());
            }
        }

        if (null != purposeLocation.get()) {
            if (TextUtils.isEmpty(purposeLocation.get().getProvinceNo())) {
                map.put("destProvinceNo", "");
            } else {
                map.put("destProvinceNo", purposeLocation.get().getProvinceNo());
            }
        }

        if (null != purposeLocation.get()) {
            if (TextUtils.isEmpty(purposeLocation.get().getCityNo())) {
                map.put("destCityNo", "");
            } else {
                map.put("destCityNo", purposeLocation.get().getCityNo());
            }
        }

        if (null != purposeLocation.get()) {
            if (TextUtils.isEmpty(purposeLocation.get().getAreaNo())) {
                map.put("destDistrictNo", "");
            } else {
                map.put("destDistrictNo", purposeLocation.get().getAreaNo());
            }
        }

        Log.v("sourceList_map", map.toString());
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarrierFilterSourceList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<CarrierListResultEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<CarrierListResultEntity>>>> call, Response<BaseEntity<ProListEntity<List<CarrierListResultEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        MToast.showToast(activity, "暂无数据");
                        return;
                    }
                    if (response.body().getData().getList() == null) {
                        MToast.showToast(activity, "暂无数据");
                        return;
                    }
                    Log.v("sourceList", response.body().getData().getList().toString());
                    activity.getCarrierInfoData(response.body().getData().getList());
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<CarrierListResultEntity>>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });

    }


    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //货车类型
    public void getCarsType() {
        APIManager.getInstance().getDriverAPI().getCarType("1").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.getCarsTypeData(response.body().getData());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_TYPE, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }


    //货车长度
    public void getCarsLength() {
        APIManager.getInstance().getDriverAPI().getCarLenType("1").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.getCarsLengthData(response.body().getData());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_LENGTH, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }


    //列表跳转H5页面详情
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getUrlSetting("").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    WebViewX5HostActivity.start(activity, url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
